package com.gc.cscott.verify;

import com.gc.cscott.domain.contest.Contest;
import com.gc.cscott.domain.contest.VerifierLocation;
import com.gc.cscott.domain.contest.numbers.TaskNumber;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import java.util.Map;

/**
 *
 * @author root
 */
public class OutputVerifierFactory {
    @Inject
    Contest contest;

    @Inject
    Injector injector;

    Map<String, Provider<OutputVerifier>> factoriesMap = ImmutableMap.<String, Provider<OutputVerifier>>builder()
            .put("Java", new Provider<OutputVerifier>() {
                public OutputVerifier get() {
                    return injector.getInstance(JavaOutputVerifier.class);
                }
            })
            .put("Groovy", new Provider<OutputVerifier>() {
                public OutputVerifier get() {
                    return injector.getInstance(GroovyOutputVerifier.class);
                }
            })
            .put("Native", new Provider<OutputVerifier>() {
                public OutputVerifier get() {
                    return injector.getInstance(NativeOutputVerifier.class);
                }
            })
            .build();

    public OutputVerifier getInstance(TaskNumber taskNumber) {
        VerifierLocation verifierLocation = contest.getVerifierLocation(taskNumber);
        final Provider<OutputVerifier> provider = factoriesMap.get(verifierLocation.getType());
        if (provider==null){
            throw new Error("Unknown judge type: "+verifierLocation.getType()+". Please check your configuration file.");
        } else {
            return provider.get();
        }
    }
}
